import { createStore } from 'vuex'
const primaryColor: String | null = window.localStorage.getItem('primaryColor')
export default createStore({
  state: {
    primaryColor: primaryColor ? primaryColor : '#504657',
  },
  mutations: {
    /** set primaryColor **/
    setPrimaryColor(state, data) {
      state.primaryColor = data
    },
  },
  actions: {
    initPrimaryColor({ state }) {
      console.log('state.primaryColor', state.primaryColor)
      ;(window as any).less
        .modifyVars({
          '@primary-color': state.primaryColor,
          '@link-color': state.primaryColor,
          '@btn-primary-bg': state.primaryColor,
        })
        .then((res: any) => {
          console.log('init res', res)
        })
        .catch((err: any) => {
          console.log('init err', err.toString())
        })
    },
    /**
     * 更换主题色
     * @param {String} value -色值
     */
    handleModifyVars({ commit }, value) {
      return new Promise((resolve, reject) => {
        console.log('value',value)
        ;(window as any).less
          .modifyVars({
            '@primary-color': value,
            '@link-color': value,
            '@btn-primary-bg': value,
          })
          .then((res: any) => {
            commit('setPrimaryColor', value)
            window.localStorage.setItem('primaryColor', value)
            resolve(res)
          })
          .catch((err: any) => {
            reject(err)
          })
      })
    },
  },
})
